<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb20">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['bill:houseBill:create']"
        >新增
        </el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          @click="handleInvalidBill"
        >未生效账单
        </el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>


    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="房间" prop="premisesAddress">
        <el-input v-model="queryParams.premisesAddress" clearable placeholder="请输入名称"></el-input>
      </el-form-item>
      <el-form-item label="承租信息" prop="userName">
        <el-input v-model="queryParams.userName" clearable placeholder="请输入名称"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="payState">
        <el-select v-model="queryParams.payState" placeholder="请选择" clearable>
          <el-option :label="item.label" :value="item.value" v-for="item in dict.type.pay_state.slice(0, 3)" :key="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="账单类型" prop="billType">
        <el-select v-model="queryParams.billType" placeholder="请选择" clearable>
          <el-option :label="item.label" :value="item.value" v-for="item in dict.type.bill_type" :key="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索
        </el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
        >重置
        </el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="list"
      ref="table"
    >
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="房间" prop="premisesAddress"/>
      <el-table-column label="账单周期" prop="name">
        <template #default="scope">
          <span>{{
              $moment(scope.row.billStartTime).format('YYYY/MM/DD')
            }} 至 {{ $moment(scope.row.billEndTime).format('YYYY/MM/DD') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="账单类型" prop="billType">
        <template #default="scope">
          <dict-tag :options="dict.type.bill_type" :value="scope.row.billType"></dict-tag>
        </template>
      </el-table-column>
      <el-table-column label="金额（元）" prop="cost"/>
      <el-table-column label="账单生成日期" prop="name">
        <template #default="scope">
          <span>{{ $moment(scope.row.billCreateTime).format('YYYY/MM/DD') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最晚付款日期" prop="name">
        <template #default="scope">
          <span>{{ $moment(scope.row.latestPayTime).format('YYYY/MM/DD') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="承租信息" prop="name">
        <template #default="scope">
          <span>{{ scope.row.userName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="name">
        <template #default="scope">
          <dict-tag :options="dict.type.pay_state" :value="scope.row.payState"></dict-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            @click="handlDetail(scope.row)"
            v-hasPermi="['bill:houseBill:query']"
          >详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleSplitBill(scope.row)"
            v-if="scope.row.payState == 3 || scope.row.payState == 1"
            v-hasPermi="['bill:houseBill:disassemble']"
          >拆分账单
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleOfflinePayment(scope.row)"
            v-if="scope.row.payState == 3 || scope.row.payState == 1"
            v-hasPermi="['bill:houseBill:offlinePayment']"
          >线下处理
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleCancel(scope.row)"
            v-if="scope.row.payState == 3 || scope.row.payState == 1"
            v-hasPermi="['bill:houseBill:logout']"
          >注销
          </el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!--  新增  -->
    <Add v-model="addDialogVisible" :title="title" :data="form" @success="getList"/>
    <!--  详情  -->
    <Detail v-model="detailVisible" :data="form" :title="title" @success="getList"/>
    <!--  线下处理  -->
    <OfflinePayment v-model="offlinePaymentVisible" :data="form" :title="title" @success="getList"/>
    <!--  未生效账单  -->
    <InvalidBill v-model="invalidBillVisible"/>
    <!--  拆分账单  -->
    <SplitBill v-model="splitBillVisible" :data="form" :title="title" @success="getList"/>
  </div>
</template>

<script>
import Add from './components/add.vue'
import Detail from './components/detail.vue'
import OfflinePayment from './components/offline-payment.vue'
import InvalidBill from './components/invalid-bill.vue'
import SplitBill from './components/split-bill.vue'

import { deleteBillItem, listBillItem, getBillItem } from '@/api/bill/item'
import { getHouseBill, listHouseBill, logoutHouseBill } from '@/api/bill/houseBill'

export default {
  name: 'lesseeSource',
  dicts: ['bill_type', 'pay_state'],
  components: {
    Add,
    Detail,
    OfflinePayment,
    InvalidBill,
    SplitBill
  },
  data() {
    return {
      showSearch: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 角色表格数据
      list: [],
      // 弹出层标题
      title: '',
      //表格行id
      id: undefined,
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 是否显示新建租客来源弹窗
      addDialogVisible: false,
      // 是否显示详情弹窗
      detailVisible: false,
      // 是否显示线下处理弹窗
      offlinePaymentVisible: false,
      // 是否显示未生效账单弹窗
      invalidBillVisible: false,
      // 是否显示拆分账单弹窗
      splitBillVisible: false,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: '', //名称
        premisesAddress: '', //房间
        payState: '' //状态
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  computed: {},
  created() {
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      listHouseBill(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.list = response.rows
          this.total = response.total
          this.loading = false
        }
      )
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.form = {}
      this.title = '新增费用信息'
      this.addDialogVisible = true

    },
    /** 未生效订单按钮操作 */
    handleInvalidBill() {
      this.invalidBillVisible = true
    },
    /** 编辑按钮操作 */
    handleUpdate(row) {
      getBillItem(row.itemId).then((response) => {
        this.form = { ...response.data }
        // this.form = { ...row }
        this.title = '编辑费用信息'
        this.addDialogVisible = true
      })

    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('确定删除该数据吗?', {}).then(() => {
        return deleteBillItem(row.itemId).then((response) => {
          this.getList()
          this.$modal.msgSuccess(response.msg)
        }).catch((error) => {
        })
      })

    },
    /** 详情按钮操作 */
    handlDetail(row) {
      getHouseBill(row.billId).then((response) => {
        this.form = { ...response.data }
        this.title = '账单详情'
        this.detailVisible = true
      })
    },
    /** 拆分账单按钮操作 */
    handleSplitBill(row) {
      getHouseBill(row.billId).then((response) => {
        this.form = { ...response.data }
        this.title = '拆分账单'
        this.splitBillVisible = true
      })
    },
    /** 线下处理按钮操作 */
    handleOfflinePayment(row) {
      this.form = { ...row }
      this.offlinePaymentVisible = true
    },
    /** 注销按钮操作 */
    handleCancel(row) {
      this.$confirm('确定注销该账单吗?', {}).then(() => {
        return logoutHouseBill(row.billId).then((response) => {
          this.$message.success(response.msg)
          this.getList()
        }).catch((error) => {
          this.$message.error(error.msg)
        })
      })
    }
  }
}
</script>
<style lang="scss" scoped>

</style>
